package com.yuxinmuyu.sports_management.controller.publicController;


import com.yuxinmuyu.sports_management.exception.R;
import com.yuxinmuyu.sports_management.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Api(value = "文件上传", tags = {"文件上传"})
@RestController
@RequestMapping("/*")
public class UploadController {
    //上传用户图像
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file) throws Exception {
        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的,从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo(new File("C:\\Users\\lenovo\\Desktop\\"+filename));
        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
        return R.success(url);
    }
}
